# Maintainer: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
_pkgbase=gdb
_targets="
	xtensa-esp8266-elf
	xtensa-esp32-elf
	xtensa-esp32s2-elf
	xtensa-esp32s3-elf
	"
pkgname="$_pkgbase-esp"
pkgver=13.2
_xtensa_overlay_ver=2021r2-patch4
pkgrel=2
pkgdesc="The GNU Debugger for embedded targets patched for ESP support"
# NOTE: gdb-multiarch replaces all the different cross-platform GDB versions
#       and supports architectures ranging from 8 bit microcontrollers
#       all the way up to super scalar multicore 64 bit server CPUs. The
#       list of supported architectures is long and even includes Xtensa.
#       However, the magic ESP MCUs are so illustrious and special, that
#       they are not to be spoken to by the common tools of mere mortals. In
#       fact, every ESP product list is so special that it needs its own magic
#       GDB version sprinkled with its own special stardust-unicorn-Espressif
#       patches to be barely worthy enough to interact with the blessed ESP
#       MCUs.
#
# tl;dr:    Please don't use this as a role model, gdb-multiarch should normally
#           be all you need. This is only needed due to crappy upstreaming
#           policy of a shitty company.
url="https://www.sourceware.org/gdb/"
# riscv64: ld: BFD (GNU Binutils) 2.39 assertion fail elfnn-riscv.c:4816 segfault
arch="all !riscv64"
license="GPL-2.0-only GPL-2.0-or-later GPL-3.0-only GPL-3.0-or-later LGPL-2.1-only"
depends="gdb"
makedepends="
	autoconf
	automake
	expat-dev
	gmp-dev
	libtool
	linux-headers
	ncurses-dev
	perl
	python3-dev
	readline-dev
	texinfo
	zlib-dev
	"
source="
	https://ftp.gnu.org/gnu/$_pkgbase/$_pkgbase-$pkgver.tar.xz
	xtensa-overlay-$_xtensa_overlay_ver.tar.gz::https://github.com/espressif/xtensa-overlays/archive/refs/tags/esp-$_xtensa_overlay_ver.tar.gz
	"
for target in $_targets; do
	targetnorm="${target//-/_}"
	subpackages="$_pkgbase-$target:$targetnorm $subpackages"
done

options="!check"
builddir="$srcdir"/$_pkgbase-$pkgver

build() {
	for target in $_targets; do
		mkdir -p "$srcdir/build/$target"
		cd "$srcdir/build/$target"

		# copy ESP specific xtensa settings from overlay
		dir="${target%%-elf}"
		dir="${dir//-/_}"
		dir="$srcdir/xtensa-overlays-esp-$_xtensa_overlay_ver/$dir/gdb"
		cp -r "$dir"/* "$builddir"/
		# add some glue for compatibility with recent GDB
		echo "xtensa_gdbarch_tdep xtensa_tdep (rmap);" \
			>> "$builddir"/gdb/xtensa-config.c

		"$builddir/configure" \
			--target=$target \
			--prefix=/usr \
			--mandir=/deleteme/man \
			--infodir=/deleteme/info \
			--pdfdir=/deleteme/pdf \
			--htmldir=/deleteme/html \
			--disable-nls \
			--disable-werror \
			--enable-interwork \
			--enable-languages=c,c++ \
			--enable-multilib \
			--with-python=/usr/bin/python3 \
			--with-system-gdbinit=/etc/gdb/gdbinit \
			--with-system-readline \
			--with-system-zlib
		make
	done
}

package() {
	for target in $_targets; do
		depends="$depends $_pkgbase-$target"
	done
	mkdir -p "$pkgdir"
}

_install_subpkg() {
	target="${subpkgname#$_pkgbase-}"
	pkgdesc="The GNU Debugger for $target"
	cd "$srcdir/build/$target"
	make DESTDIR="$subpkgdir" install

	rm -rf "$subpkgdir"/deleteme

	# those are provided by gdb
	rm -rf "${subpkgdir:?}"/usr/share
	# those are provided by binutils
	rm -rf "${subpkgdir:?}"/usr/include
	rm -rf "${subpkgdir:?}"/usr/lib
}

xtensa_esp8266_elf() {
	_install_subpkg
}

xtensa_esp32_elf() {
	_install_subpkg
}

xtensa_esp32s2_elf() {
	_install_subpkg
}

xtensa_esp32s3_elf() {
	_install_subpkg
}

sha512sums="
8185d3e11ab60dafff5860a5016577bfe7dd7547ef01ebc867bc247603d82b74ff74c4f29492c7d2aee57076f52be33e289f4c6b414a4b870d4b3004909f4c34  gdb-13.2.tar.xz
7b872a53b31ad2c02fd2b04e39b8d1c63924437f35433e2495a9dbe341a118b8754fbe087986c335a8b1c588519ad637fa73b7270a28a692ad350d58fe7bac10  xtensa-overlay-2021r2-patch4.tar.gz
"
